// pages/orderDetail/orderDetail.js
const app = getApp();
const {
  orderDetail,
  acceptOrder,
  completeOrder,
  repair,
  more,
  remark,
  saveremark,
  getremark,
  id
} = require('../../config.js');
Page({
  data: {
    data: {},
    show: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (app.globalData.orderDetailFlag == true){
      let id = options.id;
      this.setData({
        id,
      })
      app.appRequest({
        url: orderDetail,
        data: {
          id
        },
        success: (res) => {
          // console.log(res.data);
          let data = {
            ...res.data
          };
          let printer = res.data.printer;
          data.taskInfo = res.data.taskInfo.map((item) => {
            if (printer[item.printerId]) {
              item.location = printer[item.printerId].location;
              item.name = printer[item.printerId].name;
              item.printerStatus = printer[item.printerId].status;
            }
            return item
          });
          this.setData({
            data
          })
        },
        fail: (res) => {
          if (res.code == 10007) {
            wx.showToast({
              title: res.msg,
              icon: 'none',
              complete:()=>{
                setTimeout(()=>{
                  wx.navigateTo({
                    url: '/pages/workOrder/workOrder',
                  });
                },1500)
              }
            })
          }
        }
      })
    } else {
      wx.navigateTo({
        url: '/pages/login_mobile/login_mobile',
      });
    }
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  copy: function(e) {
    let phone = e.target.dataset.phone
    wx.setClipboardData({
      data: phone,
      success(res) {
        wx.showToast({
          title: '复制成功',
          icon: 'none'
        })
      }
    })
  },
  acceptOrder: function(e) {
    let id = this.data.id;
    let data = this.data.data;
    wx.showModal({
      title: `接收${data.user.name}的订单?`,
      content: `订单编号: ${data.bean.number}`,
      confirmColor: '#1E66CC',
      success(res) {
        if (res.confirm) {
          app.appRequest({
            url: acceptOrder,
            data: {
              id,
            },
            method: 'POST',
            success: (res) => {
              if (res.code == 0) {
                wx.showToast({
                  title: '接单成功',
                  icon: 'none'
                })
                wx.navigateTo({
                  url: '/pages/workOrder/workOrder?type=3',
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  completeOrder: function() {
    let id = this.data.id;
    wx.showModal({
      title: '提示',
      content: '是否确认完成订单',
      confirmColor: '#1E66CC',
      success(res) {
        if (res.confirm) {
          app.appRequest({
            url: completeOrder,
            data: {
              id,
            },
            method: 'POST',
            success: (res) => {
              if (res.code == 0) {
                wx.showToast({
                  title: '接单成功',
                  icon: 'none'
                })
                wx.navigateBack({
                  delta: 1,
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  repair: function(e) {
    let that=this;
    let id = this.data.data.bean.id;
    let printerId = e.currentTarget.dataset.printerid;
    wx.showModal({
      title: '提示',
      content: '确认该设备修复完成？',
      confirmColor: '#1E66CC',
      success(res) {
        if (res.confirm) {
          app.appRequest({
            url: repair,
            data: {
              id,
              printerId
            },
            method: 'POST',
            success: (res) => {
              if (res.code == 0) {
                app.appRequest({
                  url: orderDetail,
                  data: {
                    id
                  },
                  success: (res) => {
                    let data = {
                      ...res.data
                    };
                    let printer = res.data.printer;
                    data.taskInfo = res.data.taskInfo.map((item) => {
                      if (printer[item.printerId]) {
                        item.location = printer[item.printerId].location;
                        item.name = printer[item.printerId].name;
                        item.printerStatus = printer[item.printerId].status;
                      }
                      return item
                    })
                    that.setData({
                      data
                    })
                  }
                })
              }
            }
          })
        }
      }
    })
  },
  more: function(e) {
    let id = this.data.data.bean.id;
    let printerId = e.currentTarget.dataset.printerid;
    wx.showLoading({
      title: '加载更多',
    })
    app.appRequest({
      url: more,
      data: {
        taskId: id,
        printerId
      },
      success: (res) => {
        wx.hideLoading();
        // console.log(res);
        if (res.code == 0) {
          let data = this.data.data;
          let taskInfo = res.data.taskInfo;
          data.taskInfo.moreTaskInfo = data.taskInfo.map((item) => {
            if (item.printerId == printerId) {
              return item.moreTaskInfo = taskInfo;
            }
          })
          this.setData({
            data
          })
        }
      }
    })
  },
  //收起
  retract: function (e) {
    let printerId = e.currentTarget.dataset.printerid;
    let data = this.data.data;
    data.taskInfo.moreTaskInfo = data.taskInfo.map((item) => {
      if (item.printerId == printerId) {
        return item.moreTaskInfo=false;
      }
    })
    this.setData({
      data
    })
  },
  call: function (e) {
    let phone = e.target.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  backIndex: function () {
    wx.switchTab({
      url: '/pages/index/index',
    });
  },
  setRemark(e){
    let id = e.currentTarget.dataset.id;
    this.setData({
      show: true,
      id: id
    })
    app.appRequest({
      url: getremark,
      data: {
        id: id
      },
      success: (res) => {
        if(res.code === 0){
          this.setData({
            remark: res.data.bean.remark || ''
          })
        }else{
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  onClose() {
    this.setData({ 
      show: false ,
      id:''
    });
  },
  inputRemark(e){
    // console.log(e.detail.value)
    this.setData({
      remark: e.detail.value
    })
  },
  saveRemarkUP(){
    if(this.data.id){
      wx.showLoading({
        title: '提交中',
      })
      
      let id = this.data.id;
      let remark = this.data.remark;
      if ( !remark ) {
        wx.showToast({
          title: '请输入故障备注',
          icon: 'none'
        })
      }else{
        app.appRequest({
          url: saveremark,
          data: {
            id: id,
            remark: remark
          },
          method: 'POST',
          success: (res) => {
            wx.hideLoading();
            this.setData({
              remark: remark,
              show: false
            });
            wx.showToast({
              title: '提交成功',
            })
          }
        })
      }
    }else{
      wx.showToast({
        title: '没有任务故障id',
        icon: 'none'
      })
    }
  }
})